<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s"  uri="/struts-tags" %>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
   	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
	<title>用户管理</title>
    <link href="<%=request.getContextPath()%>/css/bootstrap.css" rel="stylesheet" type="text/css">
    <script src="<%=request.getContextPath()%>/js/jquery-2.0.3.min.js" type="text/javascript"></script>
    <script src="<%=request.getContextPath()%>/js/bootstrap.js" type="text/javascript"></script>  
    <link rel="stylesheet" href="<%=request.getContextPath()%>/css/bootstrapValidator.css"/>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/bootstrapValidator.js"></script>
    <script type="text/javascript">
    
    $(document).ready(function() {
    	
    	//添加和修改的表单验证
    	$('#defaultForm')
        .bootstrapValidator({
            message: 'This value is not valid',
            fields: {
            	"t.realname": {
                    validators: {
                        notEmpty: {
                            message: '真实姓名不能为空'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z\u4e00-\u9fa5]{2,16}$/,
                            message: '真实姓名由2-16个字母或汉字组成'
                        }
                    }
                },
                "t.role.rid": {
                    validators: {
                        notEmpty: {
                            message: '角色分配不能为空'
                        }
                    }
                },
                "t.username": {   
                    validators: {
                        notEmpty: {
                            message: '用户名不能为空'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9_]{4,10}$/,
                            message: '用户名必须由4-10个数字、字母或下划线组成'
                        },
                        remote: {
                        	//需要判断用户名不重复 ajax验证：json的返回数据必须是{"valid",true or false} false代表已存在 
                            url: '<%=request.getContextPath()%>/user/User_nameExist.action',
                            message: '用户名已存在',
                            dataType: "json", 
                            type: 'POST'
                        }
                    }
                },
                "t.password": {
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9]{6,12}$/,
                            message: '密码必须由6-12个数字或字母组成'
                        },
                        identical: {
                            field: 'qrpwd',
                            message: '请输入下行确认密码'
                        }
                    }
                },
                "qrpwd": {    
                    validators: {
                        notEmpty: {
                            message: '确认密码不能为空'
                        },
                        identical: {
                            field: 't.password',
                            message: '两次密码不一致，请再次输入密码'
                        }
                    }
                }
                
            }
        });
    	
    	
    	$('#selfForm')
        .bootstrapValidator({
            message: 'This value is not valid',
            fields: {
                "oldpwd": {    
                    validators: {
                        notEmpty: {
                            message: '原密码不能为空'
                        },
                        remote: {
                        	//需要判断密码是否正确    ajax验证：json的返回数据必须是{"valid",true or false} true代表正确 
                            url: '<%=request.getContextPath()%>/user/User_checkpwd.action',
                            message: '原密码不正确',
                            dataType: "json", 
                            type: 'POST'
                        }
                    }
                },
                "t.password": {    
                    validators: {
                        notEmpty: {
                            message: '新密码不能为空'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9]{6,12}$/,
                            message: '密码必须由6-12个数字或字母组成'
                        },
                        identical: {
                            field: 'qrnewpwd',
                            message: '请输入下行确认新密码'
                        }
                    }
                },
                "qrnewpwd": {    
                    validators: {
                        notEmpty: {
                            message: '确认新密码不能为空'
                        },
                        identical: {
                            field: 't.password',
                            message: '两次密码不一致，请再次输入密码'
                        }
                    }
                }
                
            }
        });
    	
    	
    	
    	//修改用户密码的表单验证
    	$('#userForm')
        .bootstrapValidator({
            message: 'This value is not valid',
            fields: {
                "t.password": {
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9]{6,12}$/,
                            message: '密码必须由6-12个数字或字母组成'
                        },
                        identical: {
                            field: 'qrpwd',
                            message: '请输入下行确认密码'
                        }
                    }
                },
                "qrpwd": {    
                    validators: {
                        notEmpty: {
                            message: '确认密码不能为空'
                        },
                        identical: {
                            field: 't.password',
                            message: '两次密码不一致，请再次输入密码'
                        }
                    }
                }
                
            }
        });
    });
    
    function submitSelfForm(){
    	//先进行判断
    	$("#selfForm").data("bootstrapValidator").validate();
    	var flag = $("#selfForm").data("bootstrapValidator").isValid();
    	if(flag){
    		document.getElementById("selfForm").submit();
    		alert("密码修改成功，请重新登录");
    		parent.location.href="<%=request.getContextPath()%>/login.jsp";
    	}
	}
    
    function selectRole(){
    	var rolename=$("#roleId").find("option:selected").text();
        $("#rolename").val(rolename);
    }
    

    </script>
</head>
<body>
<div class="panel panel-primary">
  <div class="panel-heading">用户管理</div>
  <div class="panel-body">
  <!-- 修改自己密码开始 -->
<c:if test="${params =='password' }">
      <form id="selfForm" class="form-horizontal" action="<%=request.getContextPath()%>/user/User_updSelfPwd.action" method="post">
		  <input type="hidden" name="t.id" value="${t.id}">
          <input type="hidden" name="t.realname" value="${t.realname}">
          <input type="hidden" name="t.role.rid" value="${t.role.rid}">
          <input type="hidden" name="t.loginip" value="${t.loginip}">
          <input type="hidden" name="t.logindate" value="${t.logindate}">
		  <input type="hidden" name="t.status" value="${t.status}">
		  <input type="hidden" name="t.addtime" value="${t.addtime}">
		  
		  
          <div class="form-group">
		    <label class="col-sm-offset-3 col-sm-2 control-label">用户名：</label>
		    <div class="col-sm-2">
		       <label class="form-control">${user.username }</label>
		       <input type="hidden" name="t.username" id="username" value="${t.username }">
		    </div>
		  </div>

          <div class="form-group">
		    <label for="t.password" class="col-sm-offset-3 col-sm-2 control-label">用户原密码：</label>
		    <div class="col-sm-2">
		        <input class="form-control" type="password" name="oldpwd" id="oldpwd" placeholder="请输入原密码">
		    </div>
		  </div>
		  
		  <div class="form-group">
		    <label for="newpassword" class="col-sm-offset-3 col-sm-2 control-label">用户新密码：</label>
		    <div class="col-sm-2">
		        <input class="form-control" type="password" name="t.password" id="password" placeholder="请输入新密码">
		    </div>
		  </div>
		  
		  <div class="form-group">
		    <label for="qrpassword" class="col-sm-offset-3 col-sm-2 control-label">确认用户新密码：</label>
		    <div class="col-sm-2">
		        <input class="form-control" type="password" name="qrnewpwd" id="qrnewpwd" placeholder="请再次输入新密码">
		    </div>
		  </div>
		  
		  <div class="form-group">
		    <div class="col-sm-offset-5 col-sm-7">
		      <button class="btn btn-default" id="tibtn" onclick="submitSelfForm()">修改</button>
		      <button type="reset" class="btn btn-default">重置</button>
		    </div>
		  </div>
       </form>
</c:if>
 <!-- 修改自己密码结束 -->
 
 
  <!-- 修改用户密码开始 -->
<c:if test="${params =='updpsw' }">
      <form id="userForm" class="form-horizontal" action="<%=request.getContextPath()%>/user/User_updUserPwd.action?page=${page}" method="post">
          <input name="method" type="hidden" value="${method }">
		  <input name="t.id" type="hidden" value="${t.id}">
          <input type="hidden" name="t.realname" value="${t.realname}">
          <input type="hidden" name="t.role.rid" value="${t.role.rid}">
          <input type="hidden" name="t.loginip" value="${t.loginip}">
          <input type="hidden" name="t.logindate" value="${t.logindate}">
		  <input type="hidden" name="t.status" value="${t.status}">
		  <input type="hidden" name="t.addtime" value="${t.addtime}">
		  
		  <!-- 为了修改完后显示角色名 -->
		  <input type="hidden" name="t.role.rolename" value="${t.role.rolename}">
		  
		  
          <div class="form-group">
		    <label class="col-sm-offset-3 col-sm-2 control-label">用户名：</label>
		    <div class="col-sm-2">
		       <label class="form-control">${t.username }</label>
		       <input type="hidden" name="t.username" id="username" value="${t.username }">
		    </div>
		  </div>

          <div class="form-group">
		    <label for="t.password" class="col-sm-offset-3 col-sm-2 control-label">新密码：</label>
		    <div class="col-sm-2">
		        <input class="form-control" type="password" name="t.password" id="password" placeholder="请输入新密码">
		    </div>
		  </div>
		  
		  <div class="form-group">
		    <label for="qrpassword" class="col-sm-offset-3 col-sm-2 control-label">确认新密码：</label>
		    <div class="col-sm-2">
		        <input class="form-control" type="password" name="qrpwd" id="qrpwd" placeholder="请再次输入新密码">
		    </div>
		  </div>
		  
		  <div class="form-group">
		    <div class="col-sm-offset-5 col-sm-7">
		      <button type="submit" class="btn btn-default">修改</button>
		      <button type="reset" class="btn btn-default">重置</button>
		    </div>
		  </div>
       </form>
</c:if>
 <!-- 修改用户密码结束 -->

  
<!-- save开始 -->
<c:if test="${params =='save' }">
	  <form id="defaultForm" class="form-horizontal" action="<%=request.getContextPath()%>/user/User_save.action?page=${page}" method="post">
	  
	   <!-- 添加开始 -->
       <c:if test="${empty method}" >
          
		 <div class="form-group">
		    <label for="t.username" class="col-sm-offset-3 col-sm-2 control-label">用户名：</label>
		    <div class="col-sm-2">
		        <input class="form-control" type="text" name="t.username" id="username" placeholder="请输入用户名">
		    </div>
		  </div>
      
		 <div class="form-group">
		    <label for="t.password" class="col-sm-offset-3 col-sm-2 control-label">用户密码：</label>
		    <div class="col-sm-2">
		        <input class="form-control" type="password" name="t.password" id="password" placeholder="请输入用户密码">
		    </div>
		  </div>
		  
		  <div class="form-group">
		    <label for="qrpassword" class="col-sm-offset-3 col-sm-2 control-label">确认密码：</label>
		    <div class="col-sm-2">
		        <input class="form-control" type="password" name="qrpwd" id="qrpwd" placeholder="请输入确认密码">
		    </div>
		  </div>
		  
		  <div class="form-group">
		    <label for="t.realname" class="col-sm-offset-3 col-sm-2 control-label">真实姓名：</label>
		    <div class="col-sm-2">
		        <input class="form-control" type="text" name="t.realname" id="realname" placeholder="请输入真实姓名">
		    </div>
		  </div>
		  
		  <div class="form-group">
		    <label for="t.role.rid" class="col-sm-offset-3 col-sm-2 control-label">角色分配：</label>
		    <div class="col-sm-2">
		        <select class="form-control" name="t.role.rid" id="roleId" onchange="selectRole()">
		            <option value="" selected="selected">请选择角色</option>
		            <s:iterator value="#roles">
		                <option value="${rid }">${rolename}</option>
		            </s:iterator>
		        </select>
		        <input type="hidden" name="t.role.rolename" id="rolename">
		    </div>
		  </div>
		  
	  </c:if>
	  <!-- 添加结束 -->
	  
	  
	  <!-- 添加修改 -->
	  <c:if test="${!empty method}" >
	      <input name="method" type="hidden" value="${method }">
		  <input name="t.id" type="hidden" value="${t.id}">
	      <input type="hidden" name="t.password" value="${t.password}">
	      <input type="hidden" name="t.loginip" value="${t.loginip}">
          <input type="hidden" name="t.logindate" value="${t.logindate}">
		  <input type="hidden" name="t.status" value="${t.status}">
		  <input type="hidden" name="t.addtime" value="${t.addtime}">
		  
		  <div class="form-group">
		    <label class="col-sm-offset-3 col-sm-2 control-label">用户名：</label>
		    <div class="col-sm-2">
               <label class="form-control">${t.username }</label>
		       <input type="hidden" name="t.username" id="username" value="${t.username }">
		    </div>
		  </div>
		  
		  <div class="form-group">
		    <label for="t.realname" class="col-sm-offset-3 col-sm-2 control-label">真实姓名：</label>
		    <div class="col-sm-2">
		        <input class="form-control" type="text" name="t.realname" id="realname" placeholder="请输入真实姓名" value="${t.realname }">
		    </div>
		  </div>
		  
		  <div class="form-group">
		    <label for="t.role.rid" class="col-sm-offset-3 col-sm-2 control-label">角色分配：</label>
		    <div class="col-sm-2">
		        <select class="form-control" name="t.role.rid" id="roleId" onchange="selectRole()">
		            <c:if test="${empty t.role.rid }">
		                <option value="" selected="selected">请选择角色</option>
		            </c:if>
		            <c:if test="${!empty t.role.rid }">
		                <option value="${t.role.rid }" selected="selected">${t.role.rolename }</option>
		            </c:if>
		            <s:iterator value="#roles">
		                <option value="${rid }">${rolename}</option>
		            </s:iterator>
		        </select>
		        <input type="hidden" name="t.role.rolename" id="rolename" value="${t.role.rolename }">
		    </div>
		  </div>
	  </c:if>
	  <!-- 修改结束 -->
	  
		  
          
		  <div class="form-group">
		    <div class="col-sm-offset-5 col-sm-7">
		      <button type="submit" class="btn btn-default">提交</button>
		      <button type="reset" class="btn btn-default">重置</button>
		    </div>
		  </div>
      </form>
</c:if>
<!-- save结束 -->    
      
      
 <!-- 查看开始 -->
<c:if test="${params =='view' }">
     <form class="form-horizontal" action="user/User_listByPage.action" method="post">
          <div class="form-group">
		      <label class="col-sm-offset-3 col-sm-2 control-label">用户名：</label>
		      <div class="col-sm-2">
		          <label class="form-control">${t.username }</label>
		      </div>
		  </div>

		  <div class="form-group">
		      <label class="col-sm-offset-3 col-sm-2 control-label">真实姓名：</label>
		      <div class="col-sm-2">
		          <label class="form-control">${t.realname }</label>
		      </div>
		  </div>

		  <div class="form-group">
		      <label class="col-sm-offset-3 col-sm-2 control-label">角色分配：</label>
		      <div class="col-sm-2">
		          <label class="form-control">${t.role.rolename }</label>
		      </div>
		  </div>

		  <div class="form-group">
		      <label class="col-sm-offset-3 col-sm-2 control-label">添加时间：</label>
		      <div class="col-sm-2">
		          <label class="form-control"><fmt:formatDate value="${t.addtime }" pattern="yyyy-MM-dd hh:mm:ss"/></label>
		      </div>
		  </div>

		  <div class="form-group">
		      <label class="col-sm-offset-3 col-sm-2 control-label">登录时间：</label>
		      <div class="col-sm-2">
		          <label class="form-control"><fmt:formatDate value="${t.logindate }" pattern="yyyy-MM-dd hh:mm:ss"/></label>
		      </div>
		  </div>
		  
		  <div class="form-group">
		      <label class="col-sm-offset-3 col-sm-2 control-label">登录IP：</label>
		      <div class="col-sm-2">
		          <label class="form-control">${t.loginip }</label>
		      </div>
		  </div>
		  
		  <div class="col-sm-offset-5 col-sm-7">
		      <button type="submit" class="btn btn-default">返回</button>
		  </div>
    </form>
</c:if>
 <!-- 查看结束 -->
    
  </div>
  
</div>

</body>
</html>
